<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="返款渠道" prop="rebatesChannel">
        <el-select
          v-model="queryParams.rebatesChannel"
          placeholder="请选择返款渠道"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in rebatesChannelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返款日期" prop="creat">
        <el-date-picker
          v-model="datePick"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
         <!-- <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
    </el-form>
   <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

  <el-tabs v-model="activeName"  type="border-card" @tab-click="handleClickTab">
    <el-tab-pane label="待返款" name="wfk">
    <el-table v-loading="loading" :data="taskData" @selection-change="handleSelectionChange">
      <el-table-column label="任务明细ID" align="center" prop="taskDetailId" />
      <el-table-column label="任务ID" align="center" prop="id" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="产品名称" align="center" prop="productName"/>
      <el-table-column label="交易量" align="center" prop="tradingVolume" />
      <el-table-column label="买家旺旺" align="center" prop="buyerNick" />
      <el-table-column label="交易类型" align="center" prop="tradingType" :formatter="tradingTypeFormat" />
      <el-table-column label="订单类型" align="center" prop="orderType" :formatter="orderTypeFormat" />
      <el-table-column label="返款类型" align="center" prop="rebatesType" :formatter="rebatesTypeFormat" />
      <el-table-column label="用户类型" align="center" prop="buyerType" :formatter="buyerTypeFormat" />
      <el-table-column label="店铺集成" align="center" prop="integration" :formatter="integrationFormat" />
      <el-table-column label="返款渠道" align="center" prop="rebatesChannel" :formatter="rebatesChannelFormat" />
      <el-table-column label="返款渠道明细" align="center" prop="rebatesWay" >
        <template slot-scope="scope">
          <el-avatar
            v-if="scope.row.rebatesChannel == 0 && scope.row.rebatesChannel != null"
            :src="fileUrl+scope.row.rebatesWay"
          ></el-avatar>
          <span v-else>{{scope.row.rebatesWay}}</span>
        </template>
      </el-table-column>
      <el-table-column label="返款参考金额" align="center" prop="rebatesAmount" >
         <template slot-scope="scope">
           <span>{{scope.row.rebatesAmount +scope.row.price }}</span>
         </template>
      </el-table-column>
      <el-table-column label="返款状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="返款责任人" align="center" prop="userName" />
      <el-table-column label="操作责任人" align="center" prop="responsibleUserName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="详情" align="center" >
        <template slot-scope="scope">
        <el-button @click="handleClick(scope.$index, scope.row)" type="text" size="small">查看详情</el-button>
      </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-tab-pane>
   <el-tab-pane label="已返款" name="yfk">
      <el-table v-loading="loading" :data="taskDataDone" @selection-change="handleSelectionChange">
      <el-table-column label="任务明细ID" align="center" prop="taskDetailId" />
      <el-table-column label="任务ID" align="center" prop="id" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="产品名称" align="center" prop="productName"/>
      <el-table-column label="交易量" align="center" prop="tradingVolume" />
      <el-table-column label="买家旺旺" align="center" prop="buyerNick" />
      <el-table-column label="交易类型" align="center" prop="tradingType" :formatter="tradingTypeFormat" />
      <el-table-column label="订单类型" align="center" prop="orderType" :formatter="orderTypeFormat" />
      <el-table-column label="返款类型" align="center" prop="rebatesType" :formatter="rebatesTypeFormat" />
      <el-table-column label="用户类型" align="center" prop="buyerType" :formatter="buyerTypeFormat" />
      <el-table-column label="店铺集成" align="center" prop="integration" :formatter="integrationFormat" />
      <el-table-column label="返款渠道" align="center" prop="rebatesChannel" :formatter="rebatesChannelFormat" />
      <el-table-column label="返款渠道明细" align="center" prop="rebatesWay" >
        <template slot-scope="scope">
          <el-avatar
            v-if="scope.row.rebatesChannel == 0 && scope.row.rebatesChannel != null"
            :src="fileUrl+scope.row.rebatesWay"
          ></el-avatar>
          <span v-else>{{scope.row.rebatesWay}}</span>
        </template>
      </el-table-column>
       <el-table-column label="返款参考金额" align="center" prop="rebatesAmount" >
         <template slot-scope="scope">
           <span>{{scope.row.rebatesAmount +scope.row.price }}</span>
         </template>
      </el-table-column>
      <el-table-column label="返款状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="返款时间" align="center" prop="updateTime" />
      <el-table-column label="返款责任人" align="center" prop="userName" />
      <el-table-column label="操作责任人" align="center" prop="responsibleUserName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="详情" align="center" >
        <template slot-scope="scope">
        <el-button @click="handleClick(scope.$index, scope.row)" type="text" size="small">查看详情</el-button>
      </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="totalDone>0"
      :total="totalDone"
      :page.sync="queryParamsDone.pageNum"
      :limit.sync="queryParamsDone.pageSize"
      @pagination="getList"
    />
    </el-tab-pane>
  </el-tabs>

    <el-dialog
      title="返款任务详情"
      :visible.sync="centerDialogVisible"
      width="960px"
      >
      <div class="detail-box">
      <el-table  :data="detailTaskList" >
          <el-table-column label="任务编号" align="center" prop="taskDetailId" />
          <el-table-column label="买家旺旺" align="center" prop="buyerNick" />
          <el-table-column label="交易类型" align="center" prop="tradingType" :formatter="tradingTypeFormat" />>
          <el-table-column label="返款渠道" align="center" prop="rebatesChannel" :formatter="rebatesChannelFormat" />
          <el-table-column label="产品金额" align="center" prop="price" />
          <el-table-column label="佣金" align="center" prop="rebatesAmount" />
          <el-table-column label="返款状态" align="center" prop="status" :formatter="statusFormat" />
        </el-table>
        <div class="jinetitle">金额 <span v-text="totalAmount"></span></div>
        <div class="position-box">
          <div class="pre-btn" @click="handlePre">
            上一条
          </div>
          <div class="next-btn" @click="handleNext">
            下一条
          </div>
          <div class="erweima-box">
          <img :src="detailTask.rebatesWay"  alt="" v-if="detailTask.rebatesChannel == '0'">
          <div class="yinhang-input" v-else>
              <textarea name="" id="" cols="30" rows="8" v-model="detailTask.rebatesWay">

              </textarea>
              <el-button type="primary">复制</el-button>
          </div>
         <el-input style="width:200px;margin-left:10px;" v-model="detailTask.remark" placeholder="备注"></el-input>
        </div>
        </div>
        <div class="querenbutton"> <el-button @click="querenfankuan" type="danger">确定返款</el-button></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { rebatestasklist,rebatestaskdetail,rebatesTask,updataRebatestaskdetail,exportReport } from "@/api/system/detail";

export default {
  name: "Detail",
  components: {
  },
  data() {
    return {
      remark:'',
      centerDialogVisible:false,
      taskData:[],
      taskDataDone:[],
      detailTaskList:[],
      detailTask:{},
      totalAmount:0,
      detailindex:0,
      activeName:'wfk',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalDone: 0,
      // 任务完成明细表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 交易类型字典
      tradingTypeOptions: [],
      // 订单类型字典
      orderTypeOptions: [],
      // 返款类型字典
      rebatesTypeOptions: [],
      // 用户类型字典
      buyerTypeOptions: [],
      // 返款渠道字典
      rebatesChannelOptions: [],
      // 状态字典
      statusOptions: [],
      fileUrl:process.env.VUE_APP_BASE_API,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        daskDate:null,
        productName:null,
        storeName:null,
        rebatesChannel:null,
        status:'1'
      },
      // 查询参数
      queryParamsDone: {
        pageNum: 1,
        pageSize: 10,
        daskDate:null,
        productName:null,
        storeName:null,
        status:'2'
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      datePick: [],
     
    };
  },
   watch: {
    datePick: {
      handler: function (val, oldval) {
        if (val != null) {
          this.queryParams.startTime = this.parseTime(val[0], "{y}-{m}-{d}");
          this.queryParams.endTime = this.parseTime(val[1], "{y}-{m}-{d}");
          this.queryParamsDone.startTime = this.parseTime(val[0], "{y}-{m}-{d}");
          this.queryParamsDone.endTime = this.parseTime(val[1], "{y}-{m}-{d}");
        }
      },
    },
  },
  created() {
    // console.log(this.$route.params.id)
    // if(this.$route.params.row){
    //    this.taskData = this.$route.params.row
    // }
    this.getList();
    this.getDicts("trading_type").then(response => {
      this.tradingTypeOptions = response.data;
    });
    this.getDicts("order_type").then(response => {
      this.orderTypeOptions = response.data;
    });
    this.getDicts("rebates_type").then(response => {
      this.rebatesTypeOptions = response.data;
    });
    this.getDicts("user_type").then(response => {
      this.buyerTypeOptions = response.data;
    });
    this.getDicts("channel_type").then(response => {
      this.rebatesChannelOptions = response.data;
    });
    this.getDicts("rebates_task_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询任务完成明细列表 */
    getList() {
      this.loading = true;
      rebatestasklist(this.queryParams).then(response => {
        this.taskData=response.rows;
        this.total = response.total;
        this.loading = false;
      });
      rebatestasklist(this.queryParamsDone).then(response => {
        this.taskDataDone=response.rows;
        this.totalDone = response.total;
        this.loading = false;
      });
    },
    // 确认返款
    querenfankuan(){
      // let item =this.taskData[this.detailindex];
      // console.log(item)
      let item=this.detailTask;
      item.status = "2"
     updataRebatestaskdetail(item).then(response=>{
        this.$message.success("操作成功！");
        // this.handleNext();
      })
    },
    // 查看详情
    handleClick(index,row){
      this.centerDialogVisible=true;
      this.detailindex=index;
      this.querytaskdetail(row.id);
    },
    handleClickTab(tab,event){
      this.getList();
    },
    querytaskdetail(id){
      rebatestaskdetail(id).then(response=>{
       let arr=[];
       arr.push(response.data)
        this.detailTaskList=arr;
        this.detailTask = arr[0];
        this.totalAmount = this.detailTask.price+this.detailTask.rebatesAmount;
      })
    },
    handlePre(){
      if(this.detailindex===0){
        this.$message('没有上一条了');
        return;
      }
      this.detailindex=this.detailindex-1;
      let id =this.taskData[this.detailindex].id;
      this.querytaskdetail(id);

    },
    handleNext(){
      let lenth= this.taskData.length;
      if(this.detailindex===lenth-1){
        this.$message('没有下一条了');
        return;
      }
      this.detailindex=this.detailindex+1;
      let id =this.taskData[this.detailindex].id;
      this.querytaskdetail(id);
    },
    // 交易类型字典翻译
    tradingTypeFormat(row, column) {
      return this.selectDictLabel(this.tradingTypeOptions, row.tradingType);
    },
    // 订单类型字典翻译
    orderTypeFormat(row, column) {
      return this.selectDictLabel(this.orderTypeOptions, row.orderType);
    },
    // 返款类型字典翻译
    rebatesTypeFormat(row, column) {
      return this.selectDictLabel(this.rebatesTypeOptions, row.rebatesType);
    },
    // 用户类型字典翻译
    buyerTypeFormat(row, column) {
      return this.selectDictLabel(this.buyerTypeOptions, row.buyerType);
    },
    integrationFormat(row, column){
      return row.integration?'是':'否'
    },
    // 返款渠道字典翻译
    rebatesChannelFormat(row, column) {
      return this.selectDictLabel(this.rebatesChannelOptions, row.rebatesChannel);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskDetailId: null,
        taskId: null,
        tradingVolume: null,
        buyerNick: null,
        tradingType: null,
        orderType: null,
        rebatesType: null,
        buyerType: null,
        rebatesChannel: null,
        rebatesWay: null,
        rebatesAmount: null,
        status: "0",
        responsibleUserId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务完成明细";
    },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = this.queryParams;
      if(this.activeName == 'yfk'){
        queryParams = this.queryParamsDone;
      }
       
      this.$confirm('是否确认导出所有任务完成明细数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportReport(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>

<style lang="scss">
.detail-table{
  width: 100%;
  td{
    line-height: 25px;
    font-size: 14px;
    text-align: left;
    border-top: 1px solid #ccc;
    padding: 0 10px;
    color: #444;
    &:nth-child(odd){
      background: #e9e9e9;
      width:100px;
      text-align: right;
      font-weight: bold;
    }
  }
}
.jinetitle{
  font-size:24px;
  font-weight: 600;
  text-align: center;
  span{
    color:red;
  }
}
.position-box{
  position: relative;
  .pre-btn,
  .next-btn{
    width: 25px;
    height: 100%;
    background: cornflowerblue;
    color: #fff;
    display: flex;
    align-items: center;
    text-align: center;
    cursor: pointer;
  }
  .next-btn{
    position: absolute;
    right: -20px;
    top: 0;
  }
  .pre-btn{
    position: absolute;
    left: -20px;
    top: 0;
  }
}
.erweima-box{
  display: flex;
  width: 100%;
  padding:0 10%;
  margin: 0 auto;
  justify-content: center;
  align-items: center;
  min-height: 100px;
  img{
    width: 200px;
  }
  .yinhang-input{
    flex: 1;
    display: flex;
    margin-left:40px;
    textarea{
      height: 100px;
      flex: 1;
    }
  }
}
.querenbutton{
  text-align:center;
  padding: 20px;
}
</style> 